package com.zzk.lanouhn.myapplication1.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.InvestPingLunAuthorAdapter;
import com.zzk.lanouhn.myapplication1.modles.FindModle;
import com.zzk.lanouhn.myapplication1.modles.InvestPingLunAuthorModle;

import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/9/18.
 */
public class InvestPingLunAuthor extends AppCompatActivity {
    private RecyclerView recyclerView;


    private InvestPingLunAuthorModle mlist;

    private InvestPingLunAuthorAdapter authorAdapter;

    private String pinglunID, fensiID, guanzhuID, ID;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invest_pinglun_author);
        recyclerView = (RecyclerView) findViewById(R.id.invest_pinglun_author_recyclerView);

        LinearLayoutManager layoutManager = new LinearLayoutManager(InvestPingLunAuthor.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        pinglunID = getIntent().getStringExtra("zuozheID");
        fensiID = getIntent().getStringExtra("FenSiID");
        guanzhuID = getIntent().getStringExtra("GuanZhuID");
        if (pinglunID != null) {
            ID = pinglunID;
        }
        if (fensiID != null) {
            ID = fensiID;
        }
        if (guanzhuID != null) {
            ID = guanzhuID;
        }

        getDataZuoZhe();

    }

    private void getDataZuoZhe() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                String url = "http://lol.zhangyoubao.com/apis/rest/UsersService/getOtherUserInfo?" +
                        "from_userid=&userid=" + ID + "&i_=K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=" +
                        "&deviceId=K5h39AQJHAgxlB3k7XOKge9pJXaie1ZTEv3udZmXycc=&t_=1474162178298&p_=" +
                        "27239&v_=400902&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=22&cha=baiduMartket&u_=";

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String ss = response.body().string();
                        Gson gson = new Gson();

                        mlist = gson.fromJson(ss, InvestPingLunAuthorModle.class);

                        handler.sendEmptyMessage(1);

                    }
                });

            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(InvestPingLunAuthor.this, "失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:

                    authorAdapter = new InvestPingLunAuthorAdapter(InvestPingLunAuthor.this, mlist);
                    recyclerView.setAdapter(authorAdapter);

                    break;


            }
        }
    };


}
